package httpbody

import (
	"fmt"
	"gitee.com/runx/mon/pkg/entry"
	"github.com/spf13/cobra"
	"github.com/valyala/fasthttp"
)

var (
	port      string
	sampleCmd = &cobra.Command{
		Use:   "httpbody",
		Short: "monitor http request body",
		RunE: func(c *cobra.Command, args []string) error {
			fasthttp.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", port), doProxy)
			return nil
		},
	}
)

func init() {
	sampleCmd.PersistentFlags().StringVarP(&port, "port", "p", "9900", "httpbody monitor port")

	entry.AddCmd(sampleCmd)
}

func doProxy(ctx *fasthttp.RequestCtx) {
	fmt.Println(string(ctx.Request.Body()))
}
